package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.http.RequestParams;
import com.sosee.cgfw.R;
import com.sosee.cgfw.adapter.UpdateAdapter;
import com.sosee.cgfw.entity.ContactJson;
import com.sosee.cgfw.myview.PickerView;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.wujay.fund.common.Constants;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Thinkpad on 2016/3/11.
 */
public class UpdatePersonalInfoActivity extends Activity implements AdapterView.OnItemClickListener {
    private EditText et;//修改
    private int from=0;//标注来自哪个界面
    private String et_context;//默认内容
    private HttpUtils httpUtils;
    private String url= Constans.update_info_url+Constans.userID;//保存信息接口
    private ListView lv;
    private UpdateAdapter adapter;
    private List<String> list;//数据
    private List<String> list_year;
    private List<String> list_month;
    private List<String> list_day;
    private PickerView pv_day;
    private PickerView pv_year;
    private PickerView pv_month;


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from=getIntent().getIntExtra("key",0);
        httpUtils=new HttpUtils();
        if(from==2||from==5){
            setContentView(R.layout.activity_update_list);
            initList();
            initViewlist();
        }else if(from==3){
            setContentView(R.layout.item_birth);
            initTime();
        }
        else{
            setContentView(R.layout.activity_update);
            initView();
            initData();
        }

    }
    public void initView(){
        et= (EditText) findViewById(R.id.et);
    }

    public void initList(){
        list=new ArrayList<String>();
        if(from==2){
            list.add("男");
            list.add("女");
        }else{
            list.add("服装设计师");
            list.add("服装院校师生");
            list.add("时装/成衣品牌商");
            list.add("经销商/买手");
            list.add("商场/商家");
            list.add("时装模特/模特经理");
            list.add("时尚编辑/记者/媒体");
            list.add("面铺料商");
            list.add("其他");
        }
    }


    public void initTime(){
        list_year=new ArrayList<String>();
        for(int i=0;i<60;i++){
            int a=1950+i;
            list_year.add(a+"年");
        }
        list_month=new ArrayList<String>();
        for (int i=1;i<13;i++){
            if(i<10){
                list_month.add(0+""+i+"月");
            }else{
                list_month.add(i+"月");
            }

        }
        list_day=new ArrayList<String>();
        for (int i=1;i<32;i++){
            if(i<10){
                list_day.add(0+""+i+"日");
            }else{
                list_day.add(i+"日");
            }

        }

        switch (from){
            case 0:
                url+="&user_nickname=";
                break;
            case 1:
                url+="&user_realname=";
                break;
            case 2:
                url+="&&user_sex=";
                break;
            case 3:
                url+="&user_birth=";
                break;
            case 4:
                url+="&user_company=";
                break;
            case 5:
                url+="&user_nickname=";
                break;
        }
        initTimerSelector();
    }



    public void initTimerSelector() {
        //测试坐标补偿
        pv_day = (PickerView) findViewById(R.id.pv_day);
        pv_day.setData(list_day);
        pv_day.setSelected(10);
        pv_year = (PickerView) findViewById(R.id.pv_year);
        pv_year.setData(list_year);
        pv_year.setSelected(6);
        pv_month = (PickerView) findViewById(R.id.pv_month);
        pv_month.setData(list_month);
        pv_month.setSelected(6);



    }


    public void initViewlist(){
        lv= (ListView) findViewById(R.id.lv);
        adapter=new UpdateAdapter(this);
        adapter.setList(list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        switch (from){
            case 0:
                url+="&user_nickname=";
                break;
            case 1:
                url+="&user_realname=";
                break;
            case 2:
                url+="&&user_sex=";
                break;
            case 3:
                url+="&user_birth=";
                break;
            case 4:
                url+="&user_company=";
                break;
            case 5:
                url+="&user_duty=";
                break;
        }
    }


    public void initData(){
        et_context=getIntent().getStringExtra("context");
        et.setText(et_context);
    }



    //点击事件
    public void click(View view){
        int id=view.getId();
        switch (id){
            //保存按钮
            case R.id.rl_save:
                Toast.makeText(this,"保存用户信息",Toast.LENGTH_SHORT).show();
//                saveInfo();
                saveInfo_post();
                break;
        }
    }

    public void onClick(View view){
        goPersonalInfo();
    }


    public void saveInfo(){
        if(from==2||from==5){

        }else if(from==3){
            //修改出生日期
            String year=list_year.get(pv_year.getmCurrentSelected());
            year=year.substring(0,year.length()-1);
            String day=list_day.get(pv_day.getmCurrentSelected());
            day=day.substring(0,day.length()-1);
            String month=list_month.get(pv_month.getmCurrentSelected());
            month=month.substring(0,month.length()-1);
            et_context=year+"-"+month+"-"+day;
        }
        else{
            //
            et_context=et.getText().toString();
        }
        LogUtils.log_E(url + "('" + et_context + "')");
        httpUtils.send(HttpRequest.HttpMethod.GET, url + "('" + et_context + "')", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    String status = jsonObject.getString("status");
                    if ("success".equals(status)) {
                        Utils.showToast("修改成功");
                        goPersonalInfo();
                    } else {
                        Utils.showToast("修改失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.internet_error));
            }
        });

    }


    //跳转 至个人信息界面
    public void goPersonalInfo(){
        Intent intent=new Intent(this,PersonalInfo.class);
        intent.putExtra("key",from);
        intent.putExtra("context",et_context);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        from=-1;
        goPersonalInfo();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        et_context=parent.getItemAtPosition(position).toString();
        saveInfo_post();
    }
    public void getParams(Map<String,String> map){
        JSONObject jsonObject=new JSONObject();
        Set<String> key=map.keySet();
        Iterator<String> it=key.iterator();
        StringBuffer sb=new StringBuffer();
        while(it.hasNext()){
            String k=it.next();
            String value=map.get(k);
            sb.append(k+"="+"'"+value+"'"+",");
        }
        try {
            jsonObject.put("userid",Constans.userID);
            jsonObject.put("data",sb.toString().substring(0,sb.length()-1));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String jso=jsonObject.toString();
        LogUtils.log_E("ceshi :"+jso);
        params=new RequestParams();
        try {
            params.setContentType("application/json");
            params.setBodyEntity(new StringEntity(jso,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
    RequestParams params;

    //post方式修改用户信息
    public void updateInfo(){
        LogUtils.log_E("测试修改url："+Constans.update_info_url);

        httpUtils.send(HttpRequest.HttpMethod.POST, Constans.update_info_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtils.log_E("测试修改用户信息："+responseInfo.result);
                goPersonalInfo();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.internet_error));
            }
        });
    }

    //post方式 保存信息
    public void saveInfo_post(){
        //处理 listview情况下的值获取
        if(et!=null){
            et_context=et.getText().toString().trim();
        }
        Map<String,String> map=new HashMap<String, String>();
        switch (from){
            case 0:
                map.put("user_nickname",et_context);
                break;
            case 1:
                map.put("user_realname",et_context);
                break;
            case 2:
                map.put("user_sex",et_context);
                break;
            case 3:
                //修改出生日期
                String year=list_year.get(pv_year.getmCurrentSelected());
                year=year.substring(0,year.length()-1);
                String day=list_day.get(pv_day.getmCurrentSelected());
                day=day.substring(0,day.length()-1);
                String month=list_month.get(pv_month.getmCurrentSelected());
                month=month.substring(0,month.length()-1);
                et_context=year+"-"+month+"-"+day;
                map.put("user_birth",et_context);
                break;
            case 4:
                map.put("user_company",et_context);
                break;
            case 5:
                map.put("user_duty",et_context);
                break;
        }
        getParams(map);
        updateInfo();
    }

}
